#!/usr/local/bin/perl -w
# Parsing log of GB, NC, CI ...
use strict;
use Template;
require JCL;


$|=1;
print "Content-type: text/html\n\n";

my $tt_file = 'result.html';

my $mmdd=$ENV{'QUERY_STRING'};

#my $mmdd = '0728';


my $dir = "/home/ftpuser/$mmdd/";
my $logdir = "/home/ftpuser/";

my $files = &JCL::get_file_list($dir);

#print "$_\n" foreach @$files;

#--- build {file => jobs} hash
#--- key is 'file' => value is 'jobs array ref.'

my %file_jobs;

foreach my $element (@$files) {
  my $jobs = &JCL::get_job_list($dir, $element);
  $file_jobs{$element} = $jobs;
}

#--- test data structure -----
#while (my ($key, $value) = each %file_jobs ) {
#  print "$key => \n";
#  print "$_\n" foreach @$value;
#}

#--- read log file every line into array
my @logs;

 $mmdd = &JCL::date_time('yesterday') unless $mmdd;

#print "mmdd=$mmdd\n";
my $log = $logdir . 'log/' . $mmdd . '.log';
#print "log file name = $log\n";
open (LOG, "< $log")
|| die "Cannot open $log: $!";

while (<LOG>) {
  @logs = <LOG>;
}
close LOG;


#-- get the analyzed period of logs
my $period;
if ($logs[0] =~ m/(\d{7}) (\d{2}:\d{2}:\d{2}\.\d{2})/) {
   $period = &JCL::date_from_yday($1) . " " . substr($2, 0, 5);
}
if ($logs[$#logs -1] =~ m/(\d{7}) (\d{2}:\d{2}:\d{2}\.\d{2})/) {
   $period = $period . ' ~ ' . &JCL::date_from_yday($1) . " " . substr($2, 0, 5);
}  


#-- build jobno => userid hash from scanning log
my $jobno_userid_ref = &JCL::scan_log_for_jobno_and_userid(\@logs);


#-- parsing log for matching job name to get Date Time JobNo JobName StepName RC
#--- build {job => found_log} hash
#--- key is 'job' => value is 'steps of this job array ref.' (Date Time JobNo JobName StepName RC)

my %found_log;

my @jcl_job_step;
#my @job_step;
my $save_jcl=' ';
my $save_job=' ';
while (my ($key, $value) = each %file_jobs ) { #for every jobs of file
#  print "$key=>\n";    # print File
  my $jcl = $key;
  foreach (@$value) {  # every Job
#    print "$_==>\n";   # print job
    my $job = $_;
    $found_log{$_}=&JCL::log_match($_, \@logs); # parsing log for job then return the array_ref for steps
    my $job_ref = $found_log{$_};    
    my $found_count = @$job_ref;
#    print "found GB22OK03! found_log count= $found_count\n" if $job eq 'GB22OK03';
    if ($found_count == 0) {
      my @job_step = ($jcl, $job, ' ', ' ', ' ', ' ', ' ', ' ');
      push @jcl_job_step, \@job_step;
    }
    else {
    foreach my $step_ref (@$job_ref) {  # for every steps details
         my @job_step;
#         my $year = substr $$step_ref[0], 0, 4;   # transfer day of year to real date
#         my $yday = substr $$step_ref[0], 4, 3;   # like 2010209 => 2010-07-28
#         my $date = &JCL::date_from_yday($year, $yday);  # sub of yday => real date
         my $date = &JCL::date_from_yday($$step_ref[0]);  # sub of yday => real date
         my $userid = $$jobno_userid_ref{$$step_ref[2]};
#         print "$date $$step_ref[1] $$step_ref[2] $$step_ref[4] $$step_ref[5] $userid\n"; 

         if ($jcl eq $save_jcl) {
           $job_step[0] = ' ';
          }
         else {
           $job_step[0] = $jcl;
           $save_jcl = $jcl;
         }
         if ($job eq $save_job) {
           $job_step[1] = ' ';
         }
         else {
           $job_step[1] = $job;
           $save_job = $job;
         }
         $job_step[2] = $date;
         $job_step[3] = $$step_ref[1];
         $job_step[4] = $$step_ref[2];
         $job_step[5] = $$step_ref[4];
         $job_step[6] = $$step_ref[5];
         $job_step[7] = $userid;
         push @jcl_job_step, \@job_step;

    }
    }
  }
}


#print "period=$period\n";

#my $count = @logs;
#print "$count\n";

my $vars = {
    joblist    => \@jcl_job_step,
##         must passing array ref. to template toolkit not array
    period     => $period,
#    now        => &JCL::date_time('now'),
#    file_name  => $file_name,
};


my $template = Template->new({
    INCLUDE_PATH => '/usr/lib/cgi-bin/src:/usr/lib/cgi-bin/lib',
    PRE_PROCESS  => 'config',
});

$template->process($tt_file, $vars)
    || die $template->error();

